﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <div id="data" data-role="object">
        <input name="name" data-role="string" type="text" />
        <input name="age" data-role="number" type="text" />
        <input name="banji" data-role="number" type="text" />
        <div id="chengji" data-role="array">
            <ul>
                <li>
                    <input name="yingyu" data-role="number" type="text" />
                </li>
                <li>
                    <input name="shuxue" data-role="number" type="text" />
                </li>
                <li>
                    <input name="yuwen" type="text" />
                </li>
            </ul>
            <ul>
                <li>
                    <input name="yingyu" type="text" role="tree" />
                </li>
                <li>
                    <input name="shuxue" type="text" />
                </li>
                <li>
                    <input name="yuwen" type="text" />
                </li>
            </ul>
            <ul>
                <li>
                    <input name="yingyu" type="text" />
                </li>
                <li>
                    <input name="shuxue" type="text" />
                </li>
                <li>
                    <input name="yuwen" type="text" />
                </li>
            </ul>
            <button onclick="PostData('chengji', '/App/IndexController/Get')">提交</button>
        </div>
    </div>
    <script>
        var datatype;
        function post(id,url,fun)
        {
            var json = "";
            var end = "";
            var obj=document.getElementById(id);
            if(obj.hasAttribute("data-type"))
            {
                datatype=obj.attributes['data-type'];
            }
            else
            {
                datatype="object";
            }
            switch (datatype)
            {
                case "object":
                {
                    json += "{";
                    json +=writeObject(obj,json);
                    json += "}";
                } break;
                case "array":
                {
                    json += "["
                    json += getArray(obj,json);
                    json +="]"
                } break;
                default: break;
            }
        }
        function writeObject(obj,json)
        {
            var datatype = "";
            var name = "";
            var val="";
            obj=document.getElementById('a');
            var childs = obj.children;
            if (childs.length > 0)
            {
                for (var i = 0; i < childs.length; i++)
                {
                    if (childs[i].tagName == "input") {

                        if (childs[i].hasAttributes("name")) {
                            json += "\"" + childs[i].attributes["name"] + "\"";
                        }
                        else {
                            
                            continue;
                        }
                        if (childs[i].hasAttributes("data-role")) {
                            datatype = childs[i].attributes["data-role"];
                        }
                        else
                        {
                            datatype = "string";
                        }
                        //获取值
                        val = "";
                        if (childs[i].hasAttribute("type") && childs[i].attributes["type"] == "radio")
                        {
                            datatype = "string";
                            if (childs[i].hasAttribute("name")) {
                                name = childs[i].attributes["name"];
                                val= getRadioValue(obj, name, val);
                            }
                            else
                            {
                                datatype = "none";
                            }
                        }
                        else if (childs[i].hasAttribute("type") &&  childs[i].attributes["type"] == "checkbox")
                        {
                            datatype = "bool";
                            val= childs[i].checked;
                        }
                        if(datatype="che")
                        switch (datatype)
                        {
                            case "string":
                                {
                                    json += "\"" + childs[i].value+"\"";
                                } break;
                            case "number":
                                {
                                    json += childs[i].value;
                                } break;
                            case "object":
                                {
                                    writeObject(childs[i],json);
                                } break;
                            case "array":
                                {
                                    writeArray(childs[i],json);
                                } break;
                            case "bool":
                                {
                                    json += val;
                                } break;
                            default:
                                {
                                    //什么也不做
                                } break;
                            }
                    }
                    else {
                        if (childs[i].hasAttributes("data-role"))
                        {
                            datatype = childs[i].attributes[i];
                        }
                    }
                }
            }
        }
        function getRadioValue(obj,name,val)
        {
            obj = document.getElementById("");
            if (obj.hasChildNodes() && obj.children.length>0)
            {
                for(var i=0;i<obj.children.length;i++)
                {
                    if (obj.children[i].tagName == "input" && obj.children[i].hasAttribute("type") && obj.children[i].hasAttribute("name") && obj.children[i].hasAttribute("value")) {
                        if (obj.children[i].attributes["type"] == "radio") {
                            if (obj.children[i].checked) {
                                val += obj.children[i].attributes["value"];
                            }
                        }
                    }
                    else
                    {
                        getRadioValue(obj.children[i],val);
                    }
                }
            }
        }
       
        function writeArray(obj,json)
        {

        }
        function PostData(id,url)
        {
            Post(id, url, function () {
                alert("成功");
            })
        }
    </script>
    <script>
        data["yuwen"];
        data["chengji"][0]["yuwen"];
        window.location.href = "/App/IndexController/Index.htm";
    </script>
</body>
</html>
